﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FontManager
{
    /// <summary>
    /// Camera class is used since what I wanted to achieve was instead of dealing with all the characters seperately in the preview window
    /// deal with them together and just change the way I see them not the exual coordinates or scale of their.
    /// </summary>
    public class Camera2D
    {
        /// <summary>
        /// the lowest limit of _zoom
        /// </summary>
            private const float zoomLowerLimit = .5f;

            private  float _zoom;
            private  Matrix _transform;
            private  Vector2 _pos;
            private  int _viewportWidth;
            private  int _viewportHeight;

            public void init(int viewportWidth,int viewportHeight)
            {
                _zoom = 1f;
                _viewportWidth = viewportWidth;
                _viewportHeight = viewportHeight;
                _pos = new Vector2(_viewportWidth / 2, _viewportHeight / 2);
            }

            #region Properties

            public float Zoom
            {
                get { return _zoom; }
                set
                {
                    _zoom = value;
                    if (_zoom < zoomLowerLimit)
                        _zoom = zoomLowerLimit;
                }
            }
        /// <summary>
        /// Position is viewport*.5 since this gives me the wanted result:
        /// zoomin based on the center of screen.
        /// </summary>
            public Vector2 Pos
            {
                get { return _pos; }
                set
                {
                    _pos = new Vector2(value.X/2, value.Y/2);
                    _viewportWidth = (int)value.X;
                    _viewportHeight = (int)value.Y;
                }
            }

            #endregion

        /// <summary>
        /// The most important part of this class.
        /// Deals with all scaling and transformation.
        /// </summary>
        /// <returns></returns>
            public Matrix GetTransformation()
            {
                _transform =
                   Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
                   Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                   Matrix.CreateTranslation(new Vector3(_viewportWidth * 0.5f,
                       _viewportHeight * 0.5f, 0));

                return _transform;
            }
    }
}
